package top.dark.persist;

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

/**
 * Author: Darkn
 * description: 萧若飞
 * Create Time: 2021/5/4 16:35
 */
public class JmsConsumer {
    private static final String ACTIVEMQ_URL = "tcp://192.168.136.181:61616";
    private static final String ACTIVEMQ_TOPIC01 = "persist_topic01";
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = factory.createConnection();
        connection.setClientID("persist_topic01");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(ACTIVEMQ_TOPIC01);
        TopicSubscriber subscriber = session.createDurableSubscriber(topic, "topice remark.....");
        connection.start();

        subscriber.setMessageListener(message->{
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("正在消费---" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
